package controllers

import (
	"errmsg"
	"errors"
	"github.com/astaxie/beego/logs"
	"github.com/astaxie/beego/validation"
	"smart_home/api/models"
)

type SceneController struct {
	BaseController
}

// @Title GetAll
// @Description 场景列表
// @router / [get]
func (c *SceneController) GetAll() {
	var (
		lists []*models.Scene
		err   error
	)

	if lists, err = models.GetAllSceneByUid(c.User.Id, 0); err != nil {
		logs.Error(err)
		c.Error(errmsg.ErrResponseSystem)
		return
	}

	c.Success(lists)
}

// @Title Get
// @Description 场景详情
// @param id path false "场景id"
// @router /:id [get]
func (c *SceneController) Get() {
	var (
		id  string
		err error
		s   *models.Scene
	)

	id = c.GetString(":id")
	if s, err = models.GetSceneById(id); err != nil {
		logs.Error(err)
		c.Error(errmsg.ErrResponseSystem)
		return
	}
	if s == nil {
		logs.Info(errmsg.ErrResponseNotFound)
		c.Error(errmsg.ErrResponseNotFound)
		return
	}

	c.Success(s)
}

// @Title Post
// @Description 添加场景
// @param name 			body true "场景自定义名称"
// @router / [post]
func (c *SceneController) Post() {
	var (
		name      string
		err       error
		is_exists bool
		vali      validation.Validation
	)

	name = c.GetString("name")
	vali = validation.Validation{}
	vali.Required(name, "name").Message("场景名称不能为空")
	vali.MinSize(name, 1, "name")
	vali.MaxSize(name, 10, "name")
	if vali.HasErrors() {
		logs.Info(err)
		c.Error(vali.Errors[0])
		return
	}

	if is_exists, err = models.IsExistsSceneByName(c.User.Id, name); err != nil {
		c.Error(errmsg.ErrResponseFail)
		return
	}
	if is_exists {
		err = errors.New("场景已存在")
		logs.Info(err)
		c.Error(err)
		return
	}

	if err = models.AddScene(c.User.Id, name); err != nil {
		c.Error(errmsg.ErrResponseFail)
		return
	}
	c.Success(nil)
}

// @Title Put
// @Description 修改场景
// @param id path false "场景id"
// @param name 			body true "场景自定义名称"
// @router /:id [put]
func (c *SceneController) Put() {
	var (
		id        string
		s         *models.Scene
		err       error
		vali      validation.Validation
		is_exists bool
	)

	id = c.GetString(":id")
	if s, err = models.GetSceneById(id); err != nil {
		c.Error(errmsg.ErrResponseSystem)
		return
	}
	if s == nil {
		logs.Info(errmsg.ErrResponseNotFound)
		c.Error(errmsg.ErrResponseNotFound)
		return
	}

	// 接收数据
	if err = c.ParseData(s); err != nil {
		c.Error(errmsg.ErrResponseParameter)
		return
	}

	vali = validation.Validation{}
	vali.Required(s.Name, "name").Message("场景名称不能为空")
	vali.MinSize(s.Name, 1, "name")
	vali.MaxSize(s.Name, 10, "name")
	if vali.HasErrors() {
		logs.Info(err)
		c.Error(vali.Errors[0])
		return
	}

	if is_exists, err = models.IsExistsSceneByName(c.User.Id, s.Name, id); err != nil {
		c.Error(errmsg.ErrResponseFail)
		return
	}
	if is_exists {
		err = errors.New("场景已存在")
		logs.Info(err)
		c.Error(err)
		return
	}

	if !models.UpdateScene(s) {
		c.Error(errmsg.ErrResponseFail)
		return
	}

	c.Success(nil)
}

// @Title Delete
// @Description 删除场景
// @param id path false "场景id"
// @router /:id [delete]
func (c *SceneController) Delete() {
	var (
		id  string
		err error
		ec  int
	)

	id = c.GetString(":id")

	// 找设备
	if ec, err = models.GetEquipmentCountByUid(c.User.Id, map[string]interface{}{"scene_id": id}); err != nil {
		c.Error(errmsg.ErrResponseFail)
		return
	}
	if ec > 0 {
		err = errors.New("请移除场景下的所有设备，再删除")
		logs.Info(err)
		c.Error(err)
		return
	}

	if !models.DelSceneById(id) {
		c.Error(errmsg.ErrResponseFail)
		return
	}

	c.Success(nil)
}
